package com.zfei.gfb.query;

import com.zfei.gfb.bean.Const;
import com.zfei.gfb.bean.Page;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author fgh
 */
public class GfbGoodsQuery extends Page {

    @ApiModelProperty("最低价格")
    private Integer minPrice;
    @ApiModelProperty("最高价格")
    private Integer maxPrice;
    @ApiModelProperty("村名")
    private String villagerName;
    @ApiModelProperty("售卖方式")
    private Integer saleType;

    @ApiModelProperty(hidden = true)
    private Integer userId;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getMinPrice() {
        return minPrice;
    }

    public void setMinPrice(Integer minPrice) {
        this.minPrice = minPrice;
    }

    public Integer getMaxPrice() {
        return maxPrice;
    }

    public void setMaxPrice(Integer maxPrice) {
        this.maxPrice = maxPrice;
    }

    public String getVillagerName() {
        return villagerName;
    }

    public void setVillagerName(String villagerName) {
        this.villagerName = villagerName;
    }

    public Integer getSaleType() {
        return saleType;
    }

    public void setSaleType(Integer saleType) {
        this.saleType = saleType;
    }


    public Page check() {
        if (minPrice != null && maxPrice != null) {
            if (minPrice <= 0 || maxPrice >= Integer.MAX_VALUE || maxPrice < minPrice) {
                throw new StatusException(Status.INVALID_ARGUMENT);
            }
        } else if (minPrice != null) {
            if (minPrice <= 0 || minPrice >= Integer.MAX_VALUE) {
                throw new StatusException(Status.INVALID_ARGUMENT);
            }
        } else if (maxPrice != null) {
            if (maxPrice <= 0 || maxPrice >= Integer.MAX_VALUE) {
                throw new StatusException(Status.INVALID_ARGUMENT);
            }
        }
        if (saleType != null && !checkType(saleType)) {
            throw new StatusException(Status.INVALID_ARGUMENT);
        }

        return this;
    }

    public static boolean checkType(Integer saleType) {
        if (saleType == null) {
            return false;
        }
        return saleType == Const.SALE_TYPE_ON || saleType == Const.SALE_TYPE_OFF;
    }
}

